package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_activity.bp_language_gender.bp_gender

import android.view.View
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.liveData
import androidx.lifecycle.switchMap
import androidx.lifecycle.viewModelScope
import com.bp.hearthealth.pulse.lnapps.R
import com.bp.hearthealth.pulse.lnapps.bp_db.bp_user.BpUserDao
import com.bp.hearthealth.pulse.lnapps.bp_extension.BpGlobalUtil
import com.bp.hearthealth.pulse.lnapps.bp_extension.LogUtil
import com.bp.hearthealth.pulse.lnapps.bp_utils.Constants
import com.bp.hearthealth.pulse.lnapps.base.BpBaseViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.Calendar

class BpGenderViewModel(
    private val userDao: BpUserDao, private val repository: BpGenderRepository
) : BpBaseViewModel() {
    var genderId = MutableLiveData(0)
    var firstOpen = MutableLiveData<Int>()
    var saveTimeStr = ""
    var showBirthday = ObservableField<String>()
    var birthdayBean = MutableLiveData<BirthdayRequestParam>()
    private var userId_ = MutableLiveData<String>()

    fun initDate() {
        genderId.value = 0
        val calendar: Calendar = Calendar.getInstance()
        val yearNow: Int = calendar.get(Calendar.YEAR)
        val monthNow = calendar.get(Calendar.MONTH) + 1
        val dayNow = calendar.get(Calendar.DAY_OF_MONTH)
        showBirthday.set("$yearNow.${Constants.monthList[monthNow - 1]} $dayNow")
        saveTimeStr = "${BpGlobalUtil.splice(yearNow)}${
            BpGlobalUtil.splice(
                monthNow
            )
        }${
            BpGlobalUtil.splice(
                dayNow
            )
        }"
        LogUtil.i("需要保存的时间为：$saveTimeStr")
    }

    fun onClick(view: View) {
        when (view.id) {
            R.id.iv_boy_bg -> {
                genderId.value = 0
            }

            R.id.iv_girl_bg -> {
                genderId.value = 1
            }
        }
    }

    fun updateIsFirstOpen() {
        viewModelScope.launch(Dispatchers.Main) {
            val update = withContext(Dispatchers.IO) {
                userDao.updateIsFirstOpen(Constants.USER_ID, false)
            }
            firstOpen.value = update
        }
    }


    /**
     * 查询指定id心率数据对象
     */
    inner class RequestParam(val month: Int, val day: Int)

    fun save(userId: String) {
        userId_.value = userId
    }

    val updateBirthdayAndGender = userId_.switchMap {
        liveData {
            val result = try {
                val code =
                    repository.updateBirthdayAndGenderInfo(it, false, saveTimeStr, genderId.value!!)
                Result.success(code)
            } catch (e: Exception) {
                Result.failure(e)
            }
            emit(result)
        }
    }

    fun save() {
        //保存数据，展示广告，广告关闭后跳转页面
        viewModelScope.launch(Dispatchers.Main) {
            withContext(Dispatchers.IO) {
                userDao.updateBirthdayInfo(Constants.USER_ID, saveTimeStr)
                userDao.updateGenderInfo(Constants.USER_ID, genderId.value!!)
                userDao.updateIsFirstOpen(Constants.USER_ID, false)
            }
        }
    }

    class BirthdayRequestParam(val year: String, val month: String, val day: String)
}